﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections.Specialized;

namespace Yuanlin.Business
{
    public class BusinessInfoAddControl : YuanlinUserControl
    {
        protected ValuedDropDownList categoryDropDownList;
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedTextBox numValuedTextBox;
        protected ValuedTextBox visitGreenNumValuedTextBox;
        protected ValuedTextBox createDateValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            BusinessInfo businessInfo = new BusinessInfo();
            
            businessInfo.CategoryId.Value = Int32.Parse(categoryDropDownList.SelectedValue);
            businessInfo.Title.Value =  CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
            businessInfo.ProductId.Value = GetEntityId(businessInfo.Title.Value, businessInfo.CategoryId.Value.ToString());
            businessInfo.Num.Value = Int32.Parse(this.numValuedTextBox.Value);
            businessInfo.UserId.Value = user.EntityId.Value;
            businessInfo.VisitGreenNum.Value =  CensorWordManager.CensorWord(this.visitGreenNumValuedTextBox.Value);
            businessInfo.CreateDate.Value = DateTime.Now;
            if (createDateValuedTextBox.Text.Trim().Length == 0)
            {
                businessInfo.CreateDate.Value = DateTime.Now;
            }
            else
            {
                businessInfo.CreateDate.Value = Globals.ChangeType<DateTime>(createDateValuedTextBox.Text);
            }

            BusinessManager.CreateBusinessInfo(businessInfo);

            if (string.IsNullOrEmpty(user.MyShopping))
            {
                user.MyShopping = "1";
            }
            else
            {
                user.MyShopping = (Int32.Parse(user.MyShopping) + 1).ToString();
            }
            UserManager.UpdateUser(user);

            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetBusinessInfoListUrl());
        }
        private void Bind()
        {
            HybridDictionary hd = new HybridDictionary();
            hd.Add((int)CategoryType.CompanyProduct, "花木");
            hd.Add((int)CategoryType.ZiCaiInfo, "资材");
            categoryDropDownList.DataSource = hd;
            categoryDropDownList.DataValueField = "key";
            categoryDropDownList.DataTextField = "value";
            categoryDropDownList.DataBind();
        }

        /// <summary>
        /// 通过类别和名称得到具体的EntityId
        /// </summary>
        /// <param name="title">名称（如常用名）</param>
        /// <param name="categoryId">如花木或资材</param>
        /// <returns>得到 公司花木或资材的 EntityId</returns>
        private int GetEntityId(string title, string categoryId)
        {
            int entityId = 0;

            if (categoryDropDownList.SelectedValue == ((int)CategoryType.CompanyProduct).ToString())
            {
                CompanyProductListRequest request = new CompanyProductListRequest();
                request.Title = title;
                EntityList companyProducts = BusinessManager.GetCompanyProductList(RequestHelper.BuildGetCompanyProductListRequest(request));
                if (companyProducts.Count>0)
                {
                    entityId = (companyProducts[0] as CompanyProduct).EntityId.Value;
                }
            }
            else if (categoryDropDownList.SelectedValue == ((int)CategoryType.ZiCaiInfo).ToString())
            {
                ZiCaiInfoListRequest request = new ZiCaiInfoListRequest();
                request.Title = title;
                EntityList ziCaiInfos = BusinessManager.GetZiCaiInfoList(RequestHelper.BuildGetZiCaiInfoListRequest(request));
                if (ziCaiInfos.Count>0)
                {
                    entityId = (ziCaiInfos[0] as ZiCaiInfo).EntityId.Value;
                }
            }

            return entityId;
        }
    }
}